<?php

namespace App\Models;

use App\Services\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class GoodsCustomRent extends Model
{
    use HasFactory;

    protected $table = 'sp_goods_custom_rent';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //获取自定义租期
    protected function GetCustomRent($spec_id){
        $data = $this->where([['spec_id',$spec_id],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->select('id','spec_id','which_period','lease_days','withhold_cycle','lease_price')->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取自定义租期
    protected function GetCustomRentBySetmealId($setmeal_id){
        $data = $this->where([['setmeal_id',$setmeal_id],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->select('id','which_period','lease_days','withhold_cycle','lease_price')->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }


    //添加自定义租金信息
    protected function AddCustomRent($goods_id,$setmeal_id,$spec_id,$custom_rent){
        if(!empty($custom_rent) && !empty($goods_id) && !empty($setmeal_id) && !empty($spec_id)){
            foreach ($custom_rent as $k=>$v){
                if(!empty($v['which_period']) && !empty($v['lease_days']) && !empty($v['lease_price'])){
                    $data = [];
                    $data['goods_id'] = $goods_id;
                    $data['setmeal_id'] = $setmeal_id;
                    $data['spec_id'] = $spec_id;
                    $data['which_period'] = $v['which_period'];
                    $data['lease_days'] = $v['lease_days'];
                    $data['withhold_cycle'] = $v['withhold_cycle'];
                    $data['lease_price'] = $v['lease_price'];
                    $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                    $this->insert($data);
                }
            }
        }
    }

    //编辑
    protected function EditCustomRent($goods_id,$setmeal_id,$spec_id,$custom_rent){
        if(!empty($custom_rent) && !empty($goods_id) && !empty($setmeal_id) && !empty($spec_id)){
            foreach ($custom_rent as $k=>$v){
                if(!empty($v['which_period']) && !empty($v['lease_days']) && !empty($v['lease_price'])){
                    $rent_data = $this->where([['goods_id',$goods_id],['setmeal_id',$setmeal_id],['spec_id',$spec_id],['which_period',$v['which_period']]])->select('id')->first();
                    $rent_data = $rent_data ? $rent_data->toArray() : array();
                    if(!empty($rent_data)){
                        $data = [];
                        $data['which_period'] = $v['which_period'];
                        $data['lease_days'] = $v['lease_days'];
                        $data['lease_price'] = $v['lease_price'];
                        $data['withhold_cycle'] = $v['withhold_cycle'];
                        $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        $this->where('id',$rent_data['id'])->update($data);
                    }else{
                        $data = [];
                        $data['goods_id'] = $goods_id;
                        $data['setmeal_id'] = $setmeal_id;
                        $data['spec_id'] = $spec_id;
                        $data['which_period'] = $v['which_period'];
                        $data['lease_days'] = $v['lease_days'];
                        $data['withhold_cycle'] = $v['withhold_cycle'];
                        $data['lease_price'] = $v['lease_price'];
                        $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
                        $this->insert($data);
                    }
                }
            }
        }
    }

}
